package kz.kstu.ds.verify;

import kz.kstu.ds.chooser.filter.KeyFileFilter;
import kz.kstu.ds.chooser.filter.XMLFileFilter;
import kz.kstu.ds.utils.GUIFactory;
import kz.kstu.ds.utils.GUIIcon;

import javax.swing.*;
import javax.xml.bind.JAXBException;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;

/**
 * Date: 04.11.12
 * Time: 23:56
 *
 * @author Oleg Anastassov
 */
public class VerifyFrame extends JFrame {
    private static final String TITLE = "Проверка подлинности документа";

    private static final int HEIGHT = 200;
    private static final int WIDTH = 500;

    private JTextField fileTextField;
    private JButton fileButton;
    private JButton keyButton;
    private JTextField keyTextField;
    private JButton verifyButton;
    private JPanel rootPanel;

    public VerifyFrame() {
        setTitle(TITLE);
        setSize(new Dimension(WIDTH, HEIGHT));
        setResizable(false);
        setContentPane(rootPanel);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        initIcons();
        initListeners();
    }

    private void initIcons() {
        setIconImage(GUIFactory.getImage(GUIIcon.LOGO));
        fileButton.setIcon(GUIFactory.getIcon(GUIIcon.CARD));
        keyButton.setIcon(GUIFactory.getIcon(GUIIcon.KEY));
        verifyButton.setIcon(GUIFactory.getIcon(GUIIcon.SHIELD));
    }

    private void initListeners() {
        fileButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser(new File("."));
                fc.setFileFilter(new XMLFileFilter());

                int result = fc.showOpenDialog(rootPanel);
                if (result == JFileChooser.APPROVE_OPTION) {
                    File f = fc.getSelectedFile();
                    fileTextField.setText(f.getAbsolutePath());
                }
            }
        });
        keyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser(new File("."));
                fc.setFileFilter(new KeyFileFilter());

                int result = fc.showOpenDialog(rootPanel);
                if (result == JFileChooser.APPROVE_OPTION) {
                    File f = fc.getSelectedFile();
                    keyTextField.setText(f.getAbsolutePath());
                }
            }
        });
        verifyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SignatureVerifier sv = new SignatureVerifier();

                try {
                    boolean isValid = sv.verify(
                            new File(fileTextField.getText()),
                            new File(keyTextField.getText())
                    );
                    if (isValid) {
                        JOptionPane.showMessageDialog(
                                rootPanel,
                                "Подпись совпадает."
                        );
                    } else {
                        JOptionPane.showMessageDialog(
                                rootPanel,
                                "Неверная подпись.",
                                "Ошибка",
                                JOptionPane.ERROR_MESSAGE
                        );
                    }
                } catch (JAXBException ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(
                            rootPanel,
                            "Подписанный файл поврежден или имеет неверный формат.",
                            "Ошибка",
                            JOptionPane.ERROR_MESSAGE
                    );
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(
                            rootPanel,
                            "Ошибка файловой системы.",
                            "Ошибка",
                            JOptionPane.ERROR_MESSAGE
                    );
                }
            }
        });
    }
}
